import os
from selenium.webdriver import Keys  # 键盘操作的模块
from common.base import Base, By, time
from common.functions import open_browser
from page.background_login_page import BackgroundLoginPage

file_path = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))  # 当前文件路径
img_path = os.path.join(file_path, 'data', 'lyy.jpg')  # 图片路径

class AddNewGoodsPage(Base):
    addnewitem_url = 'http://139.129.26.163/admin/index.php'

    # 打开新增商品
    def open_add_goods(self):
        # 切换frame
        self.swtich_iframe((By.ID, 'menu-frame'))
        self.find_ele((By.XPATH, '//*[@id="menu-ul"]/li[2]')).click()  # 管理商品
        self.find_ele((By.LINK_TEXT, '添加新商品')).click()  # 添加新商品

    # 新增商品
    # todo 通用信息
    def add_goods(self):
        # 切换frame
        self.switch_to_default_content()  # 切换到父级frame
        self.swtich_iframe((By.ID, 'main-frame'))  # 切换到指定frame

        self.find_ele((By.NAME, 'goods_name')).send_keys('懒羊羊')  # 商品名称
        self.select_by_index((By.NAME, 'goods_name_style'), 1)  # 字体样式
        # self.find_ele((By.NAME, 'goods_sn')).send_keys('1363334564')  # 商品货号
        self.select_by_index((By.NAME, 'cat_id'), 2)  # 商品分类
        self.find_ele((By.XPATH, '//*[@value="添加"]')).click()  # 扩展分类
        self.select_by_index((By.NAME, 'other_cat[]'), 3)
        self.select_by_index((By.NAME, 'brand_id'), 2)  # 商品品牌
        self.select_by_index((By.ID, 'suppliers_id'), 2)  # 选择供货商
        shop_price = self.find_ele((By.NAME, 'shop_price'))  # 本店售价
        shop_price.clear()
        shop_price.send_keys('200')
        self.find_ele((By.NAME, 'volume_number[]')).send_keys('1000')  # 优惠数量
        self.find_ele((By.NAME, 'volume_price[]')).send_keys('199')  # 优惠价格
        market_price = self.find_ele((By.NAME, 'market_price'))  # 市场价格
        market_price.clear()
        market_price.send_keys('100')
        virtual_sales = self.find_ele((By.NAME, 'virtual_sales'))  # 虚拟销量
        virtual_sales.clear()
        virtual_sales.send_keys('10000')
        give_integral = self.find_ele((By.NAME, 'give_integral'))  # 赠送消费积分数
        give_integral.clear()
        give_integral.send_keys('20')
        rank_integral = self.find_ele((By.NAME, 'rank_integral'))  # 赠送等级积分数
        rank_integral.clear()
        rank_integral.send_keys('20')
        integral = self.find_ele((By.NAME, 'integral'))  # 积分购买金额
        # TODO 键盘全选,输入新的内容,就不用clear了
        integral.send_keys(Keys.CONTROL, 'a')  # ctrl + a
        time.sleep(2)
        integral.send_keys('200')  # 替换
        time.sleep(2)
        self.find_ele((By.ID, 'is_promote')).click()  # 促销价
        promote_1 = self.find_ele((By.ID, 'promote_1'))
        promote_1.clear()
        promote_1.send_keys('180')

        # 促销日期
        # self.find_ele((By.ID, 'selbtn1')).click() # 开始日期
        # self.find_ele((By.ID, 'selbtn2')).click() # 结束日期

        self.find_ele((By.NAME, 'goods_img')).send_keys(img_path)  # 上传商品图片
        img_url = self.find_ele((By.NAME, 'goods_img_url'))
        time.sleep(2)
        img_url.clear()
        img_url.send_keys('https://baike.baidu.com/item/%E6%87%92%E7%BE%8A%E7%BE%8A/5534132?fr=kg_general')

    # todo 详细信息
    def detailed_info(self):
        self.find_ele((By.ID, 'detail-tab')).click()  # 切换到详细信息
        self.swtich_iframe((By.ID, 'goods_desc___Frame'))  # 切换到子frame
        self.swtich_iframe((By.XPATH, '//*[@id="xEditingArea"]/iframe'))  # 再切换到子frame下的iframe
        # info = self.find_ele((By.XPATH, '/html/body'))  # 定位文本框 同下面的
        info = self.find_ele((By.TAG_NAME, 'body'))  # 通过标签名定位文本框
        info.click()
        info.send_keys('青青草原上最肥的小羊之一，同时也是最懒惰的小羊，因此被灰太狼称为小胖子。')

    # 其他信息
    def other_info(self):
        self.switch_to_default_content()  # 切换到父级frame
        self.swtich_iframe((By.ID, 'main-frame'))  # 切换到指定frame

        self.find_ele((By.ID, 'mix-tab')).click()  # 切换其他信息标签
        self.find_ele((By.NAME, 'goods_weight')).send_keys('250')  # 商品重量
        self.select_by_index((By.NAME, 'weight_unit'), 1)
        goods_number = self.find_ele((By.NAME, 'goods_number'))  # 商品库存数量
        goods_number.clear()
        goods_number.send_keys('10000')
        warn_number = self.find_ele((By.NAME, 'warn_number'))  # 库存警告数量
        warn_number.clear()
        warn_number.send_keys('100')
        # 加入推荐
        # self.find_ele((By.NAME, 'is_best')).click() # 精品
        self.find_ele((By.NAME, 'is_new')).click()  # 新品
        self.find_ele((By.NAME, 'is_hot')).click()  # 热销
        # self.find_ele((By.NAME, 'is_on_sale')).click()  # 默认是上架
        # self.find_ele((By.NAME, 'is_alone_sale')).click()  # 默认作为普通商品销售
        self.find_ele((By.NAME, 'is_shipping')).click()  # 是否为免运费商品
        self.find_ele((By.NAME, 'keywords')).send_keys('好吃 懒做')  # 商品关键词
        self.find_ele((By.NAME, 'goods_brief')).send_keys('贪吃')  # 商品简单描述
        self.find_ele((By.NAME, 'seller_note')).send_keys('青青草原上最肥的小羊之一')  # 商品简单描述

    # 商品属性
    def attributes(self):
        self.find_ele((By.ID, 'properties-tab')).click()  # 切换到商品属性
        self.select_by_index((By.NAME, 'goods_type'), 3)  # 商品类型
        self.find_ele((By.XPATH, '//*[@id="attrTable"]//input[2]')).send_keys('懒羊羊')  # 中文片名
        self.find_ele((By.XPATH, '//*[@id="attrTable"]//tr[2]//input[2]')).send_keys('Paddi')  # 英文片名
        self.select_by_index((By.XPATH, '//*[@id="attrTable"]//tr[4]//select'), 3)
        self.select_by_index((By.XPATH, '//*[@id="attrTable"]//tr[5]//select'), 2)

    # 商品相册
    def goods_album(self):
        self.find_ele((By.ID, 'gallery-tab')).click()  # 切换到商品相册
        self.find_ele((By.CLASS_NAME, 'webuploader-element-invisible')).send_keys(img_path)

    # 关联商品 Associated Good
    def associated_goods(self):
        self.find_ele((By.ID, 'linkgoods-tab')).click()  # 切换到关联商品
        self.select_by_index((By.NAME, 'cat_id1'), 0)
        self.select_by_index((By.NAME, 'brand_id1'), 0)
        self.find_ele((By.NAME, 'keyword1')).send_keys('诺基亚')  # 搜索输入框
        self.find_ele((By.XPATH, '//input[@value=" 搜索 "]')).click()  # 搜索按钮
        time.sleep(2)
        self.find_ele((By.XPATH, '//input[@type="radio"][2]')).click()  # 单选框选择双向关联
        time.sleep(2)
        # self.select_by_index((By.NAME, 'source_select_by_index1'), 1)  # 选择搜索出来的第一条数据
        self.find_ele((By.XPATH, '//input[@value=">>"]')).click()  # 选择全部关联
        time.sleep(3)

    # 配件
    def accessory(self):
        self.find_ele((By.ID, 'groupgoods-tab')).click()  # 切换到配件标签
        self.select_by_value((By.NAME, 'cat_id2'), '1')  # 根据value值选择
        self.find_ele((By.NAME, 'keyword2')).send_keys('诺基亚')  # 搜索框
        self.find_ele((By.XPATH, '//*[@id="groupgoods-table"]//input[2]')).click()  # 搜索按钮
        time.sleep(2)
        self.select_by_index((By.NAME, 'source_select2'), 0)  # 选择搜索出来的数据
        time.sleep(2)
        self.find_ele((By.XPATH, '//*[@id="groupgoods-table"]//input[@value=">"]')).click()  # 关联
        time.sleep(2)
        self.find_ele((By.XPATH, '//*[@id="groupgoods-table"]//input[@value="<<"]')).click()  # 清除关联
        time.sleep(2)
        self.find_ele((By.XPATH, '//*[@id="groupgoods-table"]//input[@value=">"]')).click()  # 再次关联
        time.sleep(2)

    # 关联文章 Associated Articles
    def associated_articles(self):
        self.find_ele((By.ID, 'article-tab')).click()  # 切换到关联文章标签
        # 文章标题
        self.find_ele((By.NAME, 'article_title')).send_keys('诺基亚')  # 输入框
        self.find_ele((By.XPATH, '//*[@onclick="searchArticle()"]')).click()  # 搜索按钮
        time.sleep(2)
        self.find_ele((By.XPATH, '//*[@id="article-table"]//*[@value=">>"]')).click()  # 点击全部关联
        time.sleep(2)
        self.find_ele((By.XPATH, '//*[@id="article-table"]//*[@value="<<"]')).click()  # 清除全部关联
        time.sleep(2)
        self.select_by_index((By.NAME, 'source_select3'), 0)  # 选择搜索出来的第一条数据
        self.find_ele((By.XPATH, '//*[@id="article-table"]//*[@value=">"]')).click()  # 点击单个关联
        time.sleep(2)

    # 确定
    def submit(self):
        self.find_ele((By.XPATH, '//*[@value=" 确定 "]')).click()

    # 重置
    def resetting(self):
        self.find_ele((By.XPATH, '//*[@value=" 重置 "]')).click()

    # 打开商品列表
    def open_goods_list(self):
        self.find_ele((By.LINK_TEXT, '商品列表'))

if __name__ == '__main__':
    browser = open_browser()  # 调用functions打开浏览器
    login = BackgroundLoginPage(browser)  # 实例化后台登录
    a1 = AddNewGoodsPage(browser)  # 实例化对象
    a1.get('http://139.129.26.163/admin/index.php')  # 访问ecshop后台
    # 登录
    login.input_username()
    login.input_password()
    login.remember()
    login.submit()

    # a1.implicitly_wait()  # 调用显示等待
    a1.open_add_goods()  # 打开新增商品
    a1.add_goods()  # 通用信息
    time.sleep(2)
    a1.detailed_info()  # 详细信息
    a1.other_info()  # 其他信息
    a1.attributes()  # 商品属性
    a1.goods_album()  # 商品相册
    a1.associated_goods()  # 关联商品
    a1.accessory()  # 配件
    a1.associated_articles()  # 关联文章
    a1.submit()  # 确定
    time.sleep(2)
    a1.open_goods_list()  # 打开商品列表
    time.sleep(4)
    a1.quit()
